/**
 * 
 */
package com.hawkfly.interceptor;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.wbcs.jbsf.dao.IDataModal;
import com.wbcs.jbsf.dao.JBSFDaoTemplate;
import com.wbcs.jbsf.util.Wbcs4JBSFUtil;
import com.wbcs.system.dataimport.DataImportItem;
import com.wbcs.system.dataimport.interceptor.IDataImportInterceptor;

/**
 * @author hawkfly
 *
 */
public class ItprSDCMStudentsImportDatas implements IDataImportInterceptor
{

    /* (non-Javadoc)
     * @see com.wbcs.system.dataimport.interceptor.IDataImportInterceptor#doImportStart(com.wbcs.system.dataimport.DataImportItem)
     */
    public boolean doImportStart(DataImportItem dataImportItem)
    {
        // TODO Auto-generated method stub
        return true;
    }

    /* (non-Javadoc)
     * @see com.wbcs.system.dataimport.interceptor.IDataImportInterceptor#beforeImportRow(java.sql.Connection, com.wbcs.system.dataimport.DataImportItem, java.util.List, java.util.List)
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public boolean beforeImportRow(Connection conn,DataImportItem dataImportItem,List<String> lstColNames,List lstColValues)
    {
        List newlstColValues = new ArrayList();
        try{
	        for(int i=0;i<lstColValues.size();i++)
	        {
	            if(i == 2)//性别列
	            {
	                Object sexObj = lstColValues.get(2);
	                String sex = "1";
	                if("女".equals(sexObj)){
	                    sex = "0";
	                }
	                newlstColValues.add(sex);
	            }
	            else if(i == 7 || i == 8)//系别和专业列
	            {
	                JBSFDaoTemplate daotpl = new JBSFDaoTemplate(false);
	                newlstColValues.add(daotpl.select("select id from t_org_info where org_name=?",conn,new IDataModal<String>(){
	                    public String loadDatas(ResultSet rs,int ccount,ResultSetMetaData rmd,String dataModal) throws SQLException
	                    {
	                        while(rs.next()){
	                            dataModal = String.valueOf(rs.getInt(1));
	                        }
	                        return dataModal;
	                    }},lstColValues.get(i) == null ? "" : String.valueOf(lstColValues.get(i)).trim()));
	            }
	            else //未作任何处理的列
	            {
	                newlstColValues.add(lstColValues.get(i)); 
	            }
	        }
	        
	        int seqval = Wbcs4JBSFUtil.getnextid(conn,false,"seq_orgs"); newlstColValues.add(String.valueOf(seqval));//id
	        String passwordmd5 = Wbcs4JBSFUtil.getmd5("1");              newlstColValues.add(passwordmd5);//password
	        newlstColValues.add("0");//type
	        newlstColValues.add("0");//isadminuser
	        
	        lstColValues.clear();
	        for(int i=0;i<newlstColValues.size();i++)
	        {
	            lstColValues.add(newlstColValues.get(i));
	        }
	        
	        if(lstColValues.get(0) == null || "".equals(lstColValues.get(0)))return false;
	        else return true;
        }catch(SQLException e){
        	e.printStackTrace();
        	return false;
        }
    }

    /* (non-Javadoc)
     * @see com.wbcs.system.dataimport.interceptor.IDataImportInterceptor#afterImportRow(java.sql.Connection, com.wbcs.system.dataimport.DataImportItem, java.util.List, java.util.List)
     */
    public void afterImportRow(Connection conn,DataImportItem dataImportItem,List<String> lstColNames,List lstColValues)
    {
        // TODO Auto-generated method stub

    }

    /* (non-Javadoc)
     * @see com.wbcs.system.dataimport.interceptor.IDataImportInterceptor#doImportEnd(int, com.wbcs.system.dataimport.DataImportItem, java.lang.Exception)
     */
    public void doImportEnd(int statuscode,DataImportItem dataImportItem,Exception e)
    {
        // TODO Auto-generated method stub

    }

}

